
import isg.etsii.ull.es.UsuarioRegistrado;
import isg.etsii.ull.es.DAO.UsuarioRegistradoDAO;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ServletRegistrarse
 */
public class ServletRegistrarse extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UsuarioRegistradoDAO URDAO = null;

    @Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		
		try {
			URDAO = new UsuarioRegistradoDAO();
		} catch (ClassNotFoundException e) {
			throw new ServletException(e);
		} catch (SQLException e) {
			throw new ServletException(e);
		}	
	}
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ServletRegistrarse() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    protected void procesaPeticion(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	UsuarioRegistrado UR = new UsuarioRegistrado();
    	UR.setNUsuario(request.getParameter("user"));
    	UR.setClave(request.getParameter("cont"));
    	UR.setNombre(request.getParameter("nombre"));
    	UR.setDNI(request.getParameter("dni"));
    	
    	int correcto = 0;
    	int existe = 0;
    	
    	try {
			existe = URDAO.existe(UR.getNUsuario());
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
    	
    	if (existe == 0) {
    		try {
    			correcto = URDAO.nuevo(UR);
    		} catch (SQLException e) {
    			correcto = 0;
    		}	
    	}
    	
		if (correcto == 1) {
			UR.setClave("");
			
			HttpSession session = request.getSession();
	        session.setAttribute("UR", UR);
	        session.setAttribute("mostrarUR", "yes");
	   		
	   		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/MostrarUR.jsp");
	   		dispatcher.forward(request,response);
		}
		else {
			int error = 3;
			
			HttpSession session = request.getSession();
			
			session.setAttribute("existe", existe);
	        session.setAttribute("error", error);
	        
	        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/error.jsp");
	   		dispatcher.forward(request,response);
		}
		
    }
    
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
			RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
			dispatcher.forward(request,response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		   procesaPeticion(request,response);
	}

}



